import Vue from "vue";
import VueRouter from "vue-router";


Vue.use(VueRouter);


import home from '../views/home/home.vue'
import category from '../views/category/category.vue'
import shopcart from '../views/shopcart/shopcart.vue'
import user from '../views/user/user.vue'
import productList from '@/views/product-list/product-list.vue'
import login from '@/views/login/login.vue'
import productDetail from '@/views/product-detail/product-detail.vue'
import store from "../store";

const routes = [{
        path: '/',
        redirect: '/home'
    }, {
        path: '/home',
        component: home
    }, {
        path: '/category',
        component: category
    }, {
        path: '/shopcart',
        component: shopcart,
        meta: {
            auth: true
        }
    }, {
        path: '/user',
        component: user,
        meta: {
            auth: true
        }
    }, {
        path: '/product-list',
        component: productList
    },
    {
        path: '/login',
        component: login,
    },
    {
        path: '/product/:id',
        component: productDetail
    }
];



const router = new VueRouter({
    mode: "history",
    base: process.env.BASE_URL,
    routes,
});
router.beforeEach((to, from, next) => {
    if (to.meta.auth) {
        if (store.getters.getIsLogin) {
            next()
        } else {
            next({ path: '/login' })
        }
    } else {
        next()
    }
})

export default router;